package com.kakaxi.mybilibili.module.home.find.view;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.FindRankRecyAdapter;
import com.kakaxi.mybilibili.base.BaseFragment;
import com.kakaxi.mybilibili.bean.HorizontalVideoItemBean;
import com.kakaxi.mybilibili.module.home.find.presenter.IFindRankPresenter;
import com.kakaxi.mybilibili.module.home.find.presenter.FindRankPresenter;
import com.kakaxi.mybilibili.module.video.view.VideoActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 排行榜碎片
 * Created by kakaxi on 16/11/25.
 */

public class FindRankFragment extends BaseFragment implements IFindRankView {

    @BindView(R.id.srl_common_fragment)
    SwipeRefreshLayout swipeRefreshLayout;

    @BindView(R.id.recv_common_fragment)
    RecyclerView recyclerView;

    private FindRankRecyAdapter mAdapter;

    /** 排行榜类型参数名 **/
    private static final String PARAM_RANK_TYPE = "rankType";
    /** 标签类型参数名 **/
    private static final String PARAM_TAB_TYPE = "tabType";

    /** 排行榜类型 **/
    private int mRankType;
    /** 标签类型 **/
    private int mTabType;

    private IFindRankPresenter mPresenter;

    /**
     * 新建
     *
     * @param rankType 排行榜类型
     * @param tabType  标签类型
     * @param title    标签标题
     * @return 碎片
     */
    public static FindRankFragment newInstance(int rankType, int tabType, String title) {
        FindRankFragment rankFragment = new FindRankFragment();
        rankFragment.setTitle(title);

        // 设置传递数据
        Bundle bundle = new Bundle();
        bundle.putInt(PARAM_RANK_TYPE, rankType);
        bundle.putInt(PARAM_TAB_TYPE, tabType);
        rankFragment.setArguments(bundle);
        return rankFragment;
    }


    @Override
    protected int getContentViewId() {
        return R.layout.fragment_common_recycler;
    }

    @Override
    protected void setupViews(ViewGroup viewGroup) {
        ButterKnife.bind(this, viewGroup);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.refreshRankData(mRankType, mTabType);
            }
        });

        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int pos) {
                HorizontalVideoItemBean itemBean = mAdapter.getItem(pos);
                Bundle bundle = new Bundle();
                bundle.putString(VideoActivity.EXTRA_VIDEO_AID, String.valueOf(itemBean.getAid()));
                bundle.putString(VideoActivity.EXTRA_VIDEO_COVER_URL, itemBean.getPic());
                goActivity(VideoActivity.class, bundle);
            }
        });
    }

    @Override
    protected void initData() {
        mPresenter = new FindRankPresenter(this);

        Bundle bundle = getArguments();
        if (bundle != null) {
            mRankType = bundle.getInt(PARAM_RANK_TYPE, -1);
            mTabType = bundle.getInt(PARAM_TAB_TYPE, -1);
        }

        mPresenter.initRankData(mRankType, mTabType);
    }

    @Override
    public void showRankData(List<HorizontalVideoItemBean> horizontalVideoItemBeanList) {
        if (mAdapter == null) {
            mAdapter = new FindRankRecyAdapter(horizontalVideoItemBeanList);
            recyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.setNewData(horizontalVideoItemBeanList);
        }
    }

    @Override
    public void showLoading() {
        swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showMessage(String message) {
        toastHandler(message);
    }
}
